package cn.edu.hust.sse.software7.class3.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBAdmin;

public class EBAdminDAO {
	private static final Logger log = Logger.getLogger(EBAdminDAO.class
			.getName());

	public boolean isInited() {
		log.info("check is initial.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean result;
		try {
			tx.begin();
			Query query = pm.newQuery(EBAdmin.class);
			List<EBAdmin> admins = (List<EBAdmin>) query.execute();
			if (admins.isEmpty()) {
				log.finest("It to be initial.");
				result = false;
			} else {
				log.finest("It already has been initialed.");
				result = true;
			}
			tx.commit();
		} catch (Exception e) {
			log.info("check administator name exist occur exception!!!\n"
					+ e.getStackTrace());
			result = false;
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return result;
	}

	public boolean isExistedAdmin(String adName) {
		log.info("check administator name exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean result;
		try {
			tx.begin();
			Query query = pm.newQuery(EBAdmin.class);
			query.setFilter("adName == '" + adName + "'");
			List<EBAdmin> admins = (List<EBAdmin>) query.execute();
			if (admins.isEmpty()) {
				log.finest("check administator name " + adName + " not exist");
				result = false;
			} else {
				log.finest("check administator name " + adName
						+ "already exist.");
				result = true;
			}
			tx.commit();
		} catch (Exception e) {
			log.info("check administator name exist occur exception!!!\n"
					+ e.getStackTrace());
			result = false;
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return result;
	}

	public void addAdmin(EBAdmin admin) {
		log.info("add administrator " + admin.getAdName() + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(admin);
			tx.commit();
			log.finest("add administrator " + admin.getAdName() + " success.");
		} catch (Exception e) {
			log.info("add administator " + admin.getAdName() + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteAdmin(EBAdmin admin) {
		log.info("delete administrator " + admin.getAdName() + " begin.");
		String delName = admin.getAdName();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistent(admin);
			tx.commit();
			log.finest("delete administrator " + delName + " success.");
		} catch (Exception e) {
			log.info("delete administator " + delName + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteAllAdmin(List<EBAdmin> admins) {
		log.info("delete administrators " + admins + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistentAll(admins);
			tx.commit();
			log.finest("delete administrators  success.");
		} catch (Exception e) {
			log.info("delete administator failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void updateAdmin(EBAdmin admin) {
		log.info("modify administrator " + admin.getAdName() + " begin.");
		String adName = admin.getAdName();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			EBAdmin admin2update = pm.getObjectById(EBAdmin.class,
					admin.getAdId());
			admin2update.setDescription(admin.getDescription());
			admin2update.setEnable(admin.isEnable());
			admin2update.setPasswd(admin.getPasswd());
			pm.makePersistent(admin2update);
			tx.commit();
			log.finest("modify administrator " + adName + " success.");
		} catch (Exception e) {
			log.info("modify administator " + adName + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public boolean validateAdmin(EBAdmin admin) {
		log.info("validate administator name exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean result;
		try {
			tx.begin();
			Query query = pm.newQuery(EBAdmin.class);
			query.setFilter("adName == '" + admin.getAdName() + "'");
			query.setFilter("passwd == '" + admin.getPasswd() + "'");
			List<EBAdmin> admins = (List<EBAdmin>) query.execute();
			if (admins.isEmpty()) {
				log.finest("validate administator name " + admin.getAdName()
						+ " failure");
				result = false;
			} else {
				log.finest("validate administator name " + admin.getAdName());
				result = true;
			}
			tx.commit();
		} catch (Exception e) {
			log.info("validate administator name occur exception!!!\n"
					+ e.getStackTrace());
			result = true;
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return result;
	}

	public List<EBAdmin> getAdmins() {
		log.info("validate administator name exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBAdmin> admins = new ArrayList<EBAdmin>(2);
		try {
			tx.begin();
			Query query = pm.newQuery(EBAdmin.class);
			admins = (List<EBAdmin>) query.execute();
			if (admins.isEmpty()) {
				log.finest("there is no admins");
			} else {
				log.finest(admins.size() + " admins in total.");
			}
			tx.commit();
		} catch (Exception e) {
			log.info("get all the admins occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return admins;
	}

	public void deleteAdminById(Long adminId) {
		log.info("delete administrator with id " + adminId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			EBAdmin admin = pm.getObjectById(EBAdmin.class, adminId);
			pm.deletePersistent(admin);
			tx.commit();
			log.finest("delete administrator with id " + adminId + " success.");
		} catch (Exception e) {
			log.info("delete administator with id " + adminId + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public EBAdmin getAdminsById(Long adminId) {
		log.info("get administrator with id " + adminId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		EBAdmin admin = null;
		try {
			tx.begin();
			admin = pm.getObjectById(EBAdmin.class, adminId);
			tx.commit();
			log.finest("get administrator with id " + adminId + " success.");
		} catch (Exception e) {
			log.info("get administator with id " + adminId + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return admin;
	}
}
